<template>
  <el-dropdown
    trigger="click"
    class="international"
    @command="handleSetLanguage"
  >
    <div>
      <svg-icon
        class-name="international-icon"
        icon-class="language"
      />
    </div>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item
        :disabled="language==='zh_CNS'"
        command="zh_CNS"
      >
        中文
      </el-dropdown-item>
      <el-dropdown-item
        :disabled="language==='en_US'"
        command="en_US"
      >
        English
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
import {changeLanguages} from "../../api/system/language";
  import {EventBus} from '@/eventbus/EventBus'
import Cookies from "js-cookie";
export default {
  name: 'LangSelect',
  computed: {
    language() {
      return this.$store.getters.language
    }
  },
    beforeDestroy() {
      EventBus.$off('language-changed')
    },
  methods: {
    handleSetLanguage(lang) {
      this.$i18n.locale = lang
      this.$store.dispatch('app/setLanguage', lang)
      // localStorage.setItem('language', lang);
      //请求后端，后端切换语言
      changeLanguages(lang).then(response => {
        // 改变侧边菜单语言
        this.$store.dispatch('GenerateMenuRoutes', lang)
        // 触发language-changed事件，并传递新的语言代码
        EventBus.$emit('language-changed', lang);
        // window.location.reload();
        this.$message({
          message: this.$t("message.setLanguageSuccess"),
          type: 'success'
        })
      });
    }
  }
}
</script>

<style scoped>
.international-icon{
  width:50px;
  height: 50px;
  color:white;
}
</style>
